<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CarouselDaoImpl
 *
 * @author krisada.thiangtham
 */
require '../com.swiss.mooh.dao/CarouselDao.php';
require '../com.swiss.mooh.utils/ArrayList.php';

class CarouselDaoImpl implements CarouselDao {

    public function createButtomCarousel($carouselTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("INSERT INTO SWISS_CA_MENU VALUES(?,?,?,?,?);");
        $stmt->bind_param('sssss', $carouselTO->getCaID(), $carouselTO->getCaName(), $carouselTO->getCaLink(), $carouselTO->getCaFilePath(), $carouselTO->getCaPosition());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function createCenterCarousel($carouselTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("INSERT INTO SWISS_CA_MENU VALUES(?,?,?,?,?);");
        $stmt->bind_param('sssss', $carouselTO->getCaID(), $carouselTO->getCaName(), $carouselTO->getCaLink(), $carouselTO->getCaFilePath(), $carouselTO->getCaPosition());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function createLeftCarousel($carouselTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("INSERT INTO SWISS_CA_MENU VALUES(?,?,?,?,?);");
        $stmt->bind_param('sssss', $carouselTO->getCaID(), $carouselTO->getCaName(), $carouselTO->getCaLink(), $carouselTO->getCaFilePath(), $carouselTO->getCaPosition());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function createRightCarousel($carouselTo) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("INSERT INTO SWISS_CA_MENU VALUES(?,?,?,?,?);");
        $stmt->bind_param('sssss', $carouselTo->getCaID(), $carouselTo->getCaName(), $carouselTo->getCaLink(), $carouselTo->getCaFilePath(), $carouselTo->getCaPosition());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteButtomCarouselByID($id) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_CA_MENU WHERE CA_ID LIKE ?");
        $stmt->bind_param('s', $id);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteCenterCarouselByID($id) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_CA_MENU WHERE CA_ID LIKE ?");
        $stmt->bind_param('s', $id);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteLeftCarouselByID($id) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_CA_MENU WHERE CA_ID LIKE ?");
        $stmt->bind_param('s', $id);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteRightCarouselByID($id) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_CA_MENU WHERE CA_ID LIKE ?");
        $stmt->bind_param('s', $id);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function getAnotherCAByID($descID) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_CA_MENU WHERE CA_POSITION_TYPE LIKE ?");
        $stmt->bind_param('s', $descID);
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $caObj = new Carousel();
            $caObj->setCaID($x['CA_ID']);
            $caObj->setCaLink($x['CA_LINK']);
            $caObj->setCaName($x['CA_NAME']);
            $caObj->setCaPosition($x['CA_POSITION_TYPE']);
            $caObj->setCaFilePath($x['CA_FILE_PATH']);
            $detailList->Add($caObj);
        }
        $stmt->close();
        return $detailList;
    }

    public function getAllButtomCarousel() {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_CA_MENU WHERE CA_POSITION_TYPE LIKE 'BUTTOM'");
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $caObj = new Carousel();
            $caObj->setCaID($x['CA_ID']);
            $caObj->setCaLink($x['CA_LINK']);
            $caObj->setCaName($x['CA_NAME']);
            $caObj->setCaPosition($x['CA_POSITION_TYPE']);
            $caObj->setCaFilePath($x['CA_FILE_PATH']);
            $detailList->Add($caObj);
        }
        $stmt->close();
        return $detailList;
    }

    public function getAllCenterCarousel() {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_CA_MENU WHERE CA_POSITION_TYPE LIKE 'CENTER'");
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $caObj = new Carousel();
            $caObj->setCaID($x['CA_ID']);
            $caObj->setCaLink($x['CA_LINK']);
            $caObj->setCaName($x['CA_NAME']);
            $caObj->setCaPosition($x['CA_POSITION_TYPE']);
            $caObj->setCaFilePath($x['CA_FILE_PATH']);
            $detailList->Add($caObj);
        }
        $stmt->close();
        return $detailList;
    }

    public function getAllLeftCarousel() {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_CA_MENU WHERE CA_POSITION_TYPE LIKE 'LEFT'");
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $caObj = new Carousel();
            $caObj->setCaID($x['CA_ID']);
            $caObj->setCaLink($x['CA_LINK']);
            $caObj->setCaName($x['CA_NAME']);
            $caObj->setCaPosition($x['CA_POSITION_TYPE']);
            $caObj->setCaFilePath($x['CA_FILE_PATH']);
            $detailList->Add($caObj);
        }
        $stmt->close();
        return $detailList;
    }

    public function getAllRightCarousel() {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_CA_MENU WHERE CA_POSITION_TYPE LIKE 'RIGHT'");
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $caObj = new Carousel();
            $caObj->setCaID($x['CA_ID']);
            $caObj->setCaLink($x['CA_LINK']);
            $caObj->setCaName($x['CA_NAME']);
            $caObj->setCaPosition($x['CA_POSITION_TYPE']);
            $caObj->setCaFilePath($x['CA_FILE_PATH']);
            $detailList->Add($caObj);
        }
        $stmt->close();
        return $detailList;
    }

}
